﻿using System;
using System.Collections.Generic;
using System.Text;
using CadStrategy2013.DxfData;
using MyTool.FileOpt;

namespace CadStrategy2013.CgqStyle
{
    public class KgbClass : Cgq2013
    {
        private string m_ceng;
        private int m_bNo;
        private static Dictionary<string, string> zpw2000PeiXianDic;

        public KgbClass(string secname)
            : base(secname)
        {
        }

        //public override void GetCfg()
        //{
        //    base.GetCfg();
        //    GetCfg("开入板");
        //}
        public override List<string> ReadTemplateDxf()
        {
            return ReadDxf(Path + "\\template\\开入板.dll");
        }

        public override string GetSaveFileName(int index)
        {
            if (ShiJiTuZhiCount == 1)
                return "\\开入板.dxf";
            return "\\开入板" + (index + 1).ToString() + ".dxf";
            //if (index == 0)
            //    return "\\开入板.dxf";
            //else
            //    return "\\开入板" + index + ".dxf";
        }

        /// <summary>
        /// 得到zpw2000对应的定型配线端子对应的字典
        /// </summary>
        /// <param name="ini"></param>
        /// <returns></returns>
        private static Dictionary<string, string> GetZpw2000PeiXianDic(IniFile ini)
        {
            if (zpw2000PeiXianDic != null)
                return zpw2000PeiXianDic;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string[] strs = ini.GetString("开关板", "ZPW2000A定型").Split(new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < strs.Length; i = i + 2)
            {
                dic.Add(strs[i], strs[i + 1]);
            }
            return dic;
        }

        private void SetBoardCount_TuZhiCount()
        {
        }

        public override DxfDataClass GetDxfData()
        {
            DxfDataClass dxfDataClass = new DxfDataClass();

            IniFile inifile = new IniFile("./\\" + StationName + "\\kgb.txt");
            inifile.Load();
            Dictionary<string, string> zpw2000PeiXianDic = GetZpw2000PeiXianDic(inifile);
            string strtemp = inifile.GetString("开关板", "板号");
            try
            {
                int l = strtemp.IndexOf('-');
                m_ceng = strtemp.Substring(0, l);
                int.TryParse(strtemp.Substring(l + 1, strtemp.Length - l - 1), out m_bNo);
            }
            catch
            {
                m_ceng = "";
                m_bNo = 0;
            }

            int kgbsum0 = inifile.GetInt("开关板", "开关板数量", 0);
            for (int kgbIndex = 0; kgbIndex < kgbsum0; kgbIndex++)
            {
                int count1 = Get_Kgl_devData(dxfDataClass, inifile, kgbIndex);
                int count2 = Get_Kgl_zpw2000devData(dxfDataClass, inifile, kgbIndex);
                int count3 = 48 - count1 - count2;
                Get_Kgl_DummyDevData(dxfDataClass, inifile, kgbIndex, count3);
            }
            Set_ShiJiBanCount_ShiJiTuZhiCount(dxfDataClass, kgbsum0);
            m_dxfDataClass = dxfDataClass;
            return dxfDataClass;
        }

        private static int Get_Kgl_devData(DxfDataClass dxfDataClass, IniFile inifile, int kgbIndex)
        {
            int kgb_dev_sum = inifile.GetInt("开关板" + (kgbIndex + 1), "sum", 0);
            int count = 0;
            for (int j = 1; j <= kgb_dev_sum; j++)
            {
                count++;
                DevClass devClass = new DevClass();
                string strs = inifile.GetString("开关板" + (kgbIndex + 1), j.ToString());
                string[] s = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int n = s.Length;
                try
                {
                    devClass.DevName = s[0];
                }
                catch
                {
                    devClass.DevName = "";
                }
                try
                {
                    devClass.peiList.Add(s[1]);
                }
                catch
                {
                    devClass.peiList.Add("");
                }
                dxfDataClass.devList.Add(devClass);
            }
            return count;
        }

        private static int Get_Kgl_zpw2000devData(DxfDataClass dxfDataClass, IniFile inifile, int kgbIndex)
        {
            int kgb_dev_sum = inifile.GetInt("开关板" + (kgbIndex + 1), "sum", 0);
            string xinHaoSec = "开关板" + (kgbIndex + 1) + "\\信号点";
            int zpw2000_dev_sum = inifile.GetInt(xinHaoSec, "sum", 0);
            Dictionary<string, string> dic = GetZpw2000PeiXianDic(inifile);
            string[] caiJiName = inifile.GetString(xinHaoSec, "zpw2000采集开关量信息").Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int i = 1;
            int count = 0;
            int sum = kgb_dev_sum + zpw2000_dev_sum > 48 ? 48 : kgb_dev_sum + zpw2000_dev_sum;
            for (int j = kgb_dev_sum; j < sum; j++)
            {
                string strs = inifile.GetString(xinHaoSec, (i++).ToString());
                string[] s = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                //int n = s.Length;
                foreach (string name in caiJiName)
                {
                    if (s.Length > 1 && s[1] == "0" && (name == "-L" || name == "-U" || name == "-H"))
                        continue;
                    count++;
                    DevClass devClass = new DevClass();
                    try
                    {
                        devClass.DevName = s[0] + name;
                    }
                    catch
                    {
                        devClass.DevName = "";
                    }
                    try
                    {
                        devClass.peiList.Add(s[2] + dic[name]);
                    }
                    catch
                    {
                        devClass.peiList.Add("");
                    }

                    dxfDataClass.devList.Add(devClass);
                }
            }
            return count;
        }

        private static void Get_Kgl_DummyDevData(DxfDataClass dxfDataClass, IniFile inifile, int kgbIndex, int dummySum)
        {
            //int kgb_dev_sum = inifile.GetInt("开关板" + (kgbIndex + 1), "sum", 0);
            //string xinHaoSec = "开关板" + (kgbIndex + 1) + "\\信号点";
            //int zpw2000_dev_sum = inifile.GetInt(xinHaoSec, "sum", 0);
            //int caiJiName_sum = GetZpw2000PeiXianDic(inifile).Count;
            //for (int i1 = 1; i1 <= dummySum; i1++)
            //{
            //}
            //int sum = kgb_dev_sum + zpw2000_dev_sum > 48 ? 48 : kgb_dev_sum + zpw2000_dev_sum;
            if (dummySum <= 0) return;
            for (int i = 1; i <= dummySum; i++)
            {
                DevClass devClass = new DevClass();
                devClass.DevName = "";
                devClass.peiList.Add("");
                dxfDataClass.devList.Add(devClass);
            }
        }

        /// <summary>
        /// 计算实际板数和实际图纸数
        /// </summary>
        /// <param name="dxfDataClass"></param>
        /// <param name="kgbsum0"></param>
        private void Set_ShiJiBanCount_ShiJiTuZhiCount(DxfDataClass dxfDataClass, int kgbsum0)
        {
            if (kgbsum0 != 0)
            {
                ShiJiBanCount = (dxfDataClass.devList.Count - 1) / BanDevCount + 1;
                ShiJiTuZhiCount = (ShiJiBanCount - 1) / BanCount + 1;
            }
            else
            {
                ShiJiBanCount = 0;
                ShiJiTuZhiCount = 0;
            }
        }

        public override string ReplacebNo(string str, int fi = 0)
        {
            //return base.ReplacebNo(str, fi);
            string str1 = str;
            for (int i = 0; i < BanCount; i++)
            {
                string str2 = "CX-" + (i + 2);
                if (str.Contains(str2))
                {
                    if (ShiJiBanCount == 0)
                    {
                        string s = str;
                        return s.Replace(str2, "");
                        // return s;
                    }
                    return str.Replace(str2, GetCengNo(i, fi));
                    //return str;
                }
            }
            return str1;
        }

        private string GetCengNo(int n, int fi)
        {
            int cengNo;
            int.TryParse(m_ceng.Substring(1, m_ceng.Length - 1), out cengNo);
            int m = n + fi * BanCount;
            if (m > ShiJiBanCount)
                return "";
            int n1 = m + m_bNo;
            if (m_ceng == "C1")
            {
                if (n1 >= 8)
                {
                    int cn = (n1 - 8) / 8 + 2;
                    int ws = (n1 - 8) % 8 + 2;
                    return "C" + cn + "-" + ws;
                }
                else
                    return "C1-" + n1;
            }
            else
            {
                int cn = (n1 - 2) / 8 + cengNo;
                int ws = (n1 - 2) % 8 + 2;
                return "C" + cn + "-" + ws;
            }
        }

        //private string GetNextCeng(string ceng)
        //{
        //    string str = "";
        //    switch (ceng)
        //    {
        //        case "C1":
        //            str = "C2";
        //            break;
        //        case "C2":
        //            str = "C3";
        //            break;
        //        case "C3":
        //            str = "C4";
        //            break;
        //        case "C4":
        //            str = "C5";
        //            break;
        //    }
        //    m_bNo = 2;
        //    return  str;
        //}
        //public override string ReplacePei(string str, int fi = 0)
        //{
        //    for (int j = 1; j <= BanCount; j++)
        //    {
        //        int nj = j;// -fi * BanCount;
        //        for (int i = 1; i <= BanDevPeiCount * BanDevCount; i++)
        //        {
        //            string str1 = "pei" + nj.ToString() + "-" + i.ToString() + ">";
        //            if (str.Contains(str1))
        //            {
        //                int index = (fi * BanCount * BanDevCount+(j-1)*BanDevCount+i-1);// (j - 1) * BanDevCount + (i - 1) / BanDevPeiCount;
        //                if (index >= m_dxfDataClass.devList.Count)
        //                    return "";
        //                int index1 = (i - 1) % BanDevPeiCount;
        //                try
        //                {
        //                    return str = str.Replace(str1, m_dxfDataClass.devList[index].peiList[index1]);
        //                }
        //                catch (Exception e)
        //                {
        //                    return "";
        //                }
        //            }
        //        }
        //    }
        //    return str;
        //}
    }
}